package com.inkFlow.crawl.controller;

import java.util.List;

import com.inkFlow.common.core.domain.model.LoginUser;
import com.inkFlow.common.excel.utils.ExcelUtil;
import com.inkFlow.common.idempotent.annotation.RepeatSubmit;
import com.inkFlow.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.inkFlow.common.log.annotation.Log;
import com.inkFlow.common.web.core.BaseController;
import com.inkFlow.common.mybatis.core.page.PageQuery;
import com.inkFlow.common.core.domain.R;
import com.inkFlow.common.core.validate.AddGroup;
import com.inkFlow.common.core.validate.EditGroup;
import com.inkFlow.common.log.enums.BusinessType;
import com.inkFlow.crawl.domain.vo.CrawlSourceConfigVo;
import com.inkFlow.crawl.domain.bo.CrawlSourceConfigBo;
import com.inkFlow.crawl.service.ICrawlSourceConfigService;
import com.inkFlow.common.mybatis.core.page.TableDataInfo;

/**
 * 爬虫管理
 *
 * @author inkFlow
 * @date 2025-08-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/crawl/config")
public class CrawlSourceConfigController extends BaseController {

    private final ICrawlSourceConfigService crawlSourceConfigService;

    /**
     * 查询爬虫管理列表
     */
    @SaCheckPermission("crawl:crawlSourceConfig:list")
    @GetMapping("/list")
    public TableDataInfo<CrawlSourceConfigVo> list(CrawlSourceConfigBo bo, PageQuery pageQuery) {
        return crawlSourceConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出爬虫管理列表
     */
    @SaCheckPermission("crawl:crawlSourceConfig:export")
    @Log(title = "爬虫管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CrawlSourceConfigBo bo, HttpServletResponse response) {
        List<CrawlSourceConfigVo> list = crawlSourceConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "爬虫管理", CrawlSourceConfigVo.class, response);
    }

    /**
     * 获取爬虫管理详细信息
     *
     * @param crawlId 主键
     */
    @SaCheckPermission("crawl:crawlSourceConfig:query")
    @GetMapping("/{crawlId}")
    public R<CrawlSourceConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer crawlId) {
        return R.ok(crawlSourceConfigService.queryById(crawlId));
    }

    /**
     * 新增爬虫管理
     */
    @SaCheckPermission("crawl:CrawlSourceConfig:add")
    @Log(title = "爬虫管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CrawlSourceConfigBo bo) {
        return toAjax(crawlSourceConfigService.insertByBo(bo));
    }

    /**
     * 修改爬虫管理
     */
    @SaCheckPermission("crawl:crawlSourceConfig:edit")
    @Log(title = "爬虫管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CrawlSourceConfigBo bo) {
        return toAjax(crawlSourceConfigService.updateByBo(bo));
    }

    /**
     * 删除爬虫管理
     *
     * @param crawlIds 主键串
     */
    @SaCheckPermission("crawl:crawlSourceConfig:remove")
    @Log(title = "爬虫管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{crawlIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] crawlIds) {
        return toAjax(crawlSourceConfigService.deleteWithValidByIds(List.of(crawlIds), true));
    }
    /**
     * 开启爬虫
     *
     * @param crawlId 主键
     */
    @SaCheckPermission("crawl:crawlSourceConfig:edit")
    @Log(title = "爬虫管理", businessType = BusinessType.UPDATE)
    @PostMapping("/open")
    public R<Void> openCrawl(@NotNull(message = "主键不能为空") @RequestBody Integer crawlId) {
        return crawlSourceConfigService.openOrCloseCrawl(crawlId, true);
    }
    /**
     * 关闭爬虫
     *
     * @param crawlId 主键
     */
    @SaCheckPermission("crawl:crawlSourceConfig:edit")
    @Log(title = "爬虫管理", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public R<Void> closeCrawl(@NotNull(message = "主键不能为空") @RequestBody Integer crawlId) {
        return crawlSourceConfigService.openOrCloseCrawl(crawlId, false);
    }
}
